import time
import pyautogui
import cv2
import location

left = 1080
top = 308

# 点击按钮
def click(cut_main_gray, type):
    print("点击", type)
    if type == "allgoods":
        return handleclick(cut_main_gray, 'allgoods.png', 10, 3)
    elif type == "next":
        return handleclick(cut_main_gray, 'xgfl.png', 10, 20)

# 执行按钮点击
def handleclick(cut_main_gray, img, left, top):
    img_gray = cv2.cvtColor(cv2.imread('./scene/' + img), cv2.COLOR_BGR2GRAY)
    # 将结果再次处理，仅保留坐标值
    locations = location.getlocations(cut_main_gray, img_gray)
    if len(locations) > 0:
        clickpostion(locations[0][0] + left, locations[0][1] + top)
        time.sleep(2)
        return True
    return False

# 点击指定坐标，加上偏移量
def clickpostion(x, y):
    pyautogui.click(left + x, top + y)

def moveTo(x, y):
    pyautogui.moveTo(left + x, top + y)

# 点击指定坐标，加上偏移量
def dragTo(x, y, t):
    pyautogui.dragTo(left + x, top + y, t)

def keyup(key):
    time.sleep(0.01)
    pyautogui.keyUp(key)

def keydown(key):
    time.sleep(0.01)
    pyautogui.keyDown(key)

def scroll(clicks):
    time.sleep(0.01)
    pyautogui.scroll(clicks)

def reset():
    # 关闭小程序
    pyautogui.click(1550, 167)
    time.sleep(1)
    # 刷新我的小程序
    pyautogui.click(81, 24)
    time.sleep(2)
    pyautogui.rightClick(720, 190)
    time.sleep(0.5)
    # 删除小程序
    pyautogui.click(749, 307)
    time.sleep(0.5)
    # 打开微信
    #pyautogui.click(366, 1426)
    pyautogui.click(417, 1426)
    time.sleep(0.5)
    # 进入小程序
    pyautogui.click(1540, 800)

if __name__ == '__main__':
    reset()